let http = require('../../../utils/api.js');
let domain = require('../../../utils/api.js');
import {
    myHttps
} from '../../../utils/util.js';
Page({
    data: {
        news: false, //控制留言墙得显示隐藏
        finish: true, //控制已创建得显示隐藏
        items: [], //主题页面得所有数据
        startX: 0, //开始坐标
        startY: 0,
        hiddenLoading: false,
        userId: '', //用户得uid
        current: 1, //当前页
        size: 10, //页容量
        domain: '', //ip
    },

    onLoad: function(e) {
        // console.log(e.userId)
        this.setData({
            userId: e.userId,
            domain: domain.domain
        })
        this.myfollow(e.userId, this.data.current, this.data.size);
    },
    //点击订阅每一条 跳转到主题主页
    goThemeHome(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../../themeHome/themeHome?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    // 获取我的订阅列表 接口
    myfollow(id, page, size) {
        let url = http.api.myfollow + id + '/fav/topics?page=' + page + '&size=' + size;
        var that = this;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                // console.log(res.data.content)
                // for (var i = 0; i < res.data.content.length; i++) {
                //     this.data.items.push(res.data.content[i])
                // }
                that.setData({
                    items: this.data.items.concat(res.data.content),
                    hiddenLoading: true,
                    length: res.data._meta.total
                });
                if (res.data._meta.total > 0) {
                    wx.setNavigationBarTitle({
                        title: '我的订阅  ( ' + this.data.length + ' ）' //页面标题为路由参数
                    })
                } else if (res.data._meta.total <= 0) {
                    wx.setNavigationBarTitle({
                        title: '我的订阅' //页面标题为路由参数
                    })
                }
            }
        }).catch(err => {})
    },
    //添加订阅 关注主题接口
    addgz(e) {
        this.setData({
            hiddenLoading: false
        })
        let that = this
        var id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        // console.log(e)
        // 走关注的接口 
        let url = http.api.followtheme + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            // 关注成功后 改变状态
            if (res.code == 1) {
                this.data.items[index].subscribed = true,
                    this.data.items[index].fav_count = this.data.items[index].fav_count + 1
                that.setData({
                    items: this.data.items,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    // 取消订阅
    cancelgz(e) {
        let that = this
        var id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index;
        // 走取消关注的接口 
        let url = http.api.delFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE"
        }).then(res => {
            if (res.code == 1) {
                // 取消关注后 改变状态 
                this.data.items[index].subscribed = false,
                    this.data.items[index].fav_count = this.data.items[index].fav_count - 1
                that.setData({
                    items: this.data.items,
                    hiddenLoading: true
                })
            }
        }).catch(err => {})
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function() {
        this.setData({
            items: []
        })
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            size: 10,
            current: 1,
            hiddenLoading: false
        })

        this.myfollow(this.data.userId, this.data.current, this.data.size);
    },
    // 上拉加载数据
    onReachBottom: function() {
        if (this.data.length > 0) {
            this.setData({
                size: 10,
                current: this.data.current + 1,
            })
            this.myfollow(this.data.userId, this.data.current, this.data.size);
        }
    },
})